package com.zyh.demo.reflect;

import com.zyh.demo.domain.Book;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Properties;

/**
 * @Author zyh
 * @Date 2024/8/14 14:18
 * @Description 模拟spring的IOC容器
 */

public class ReflectTest3 {
    static {
        try {
            //创建一个properties集合
            Properties prop = new Properties();
            //通过类加载器获取流
            prop.load(ReflectTest3.class.getClassLoader().getResourceAsStream("spring.properties"));
            //获取集合中的value
            String className = (String) prop.get("className");
            String methodName = (String) prop.get("methodName");
            //获取类对象
            Class c = Class.forName(className);
            //获取方法名
            Method method = c.getDeclaredMethod(methodName);
            //获取构造方法并且创建对象
            Constructor cons = c.getDeclaredConstructor(String.class, String.class, Double.class, Date.class);
            Book book = (Book) cons.newInstance("水浒传", "zyh", 20.9, new Date());
            //通过toString方法输出
            System.out.println(method.invoke(book));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {

    }
}
